<template>
  <div ref="chartRef" class="chart-container"></div>
</template>

<script setup>
import { ref, onMounted, onUnmounted, watch } from 'vue';
import * as echarts from 'echarts/core';
import { PieChart } from 'echarts/charts';
import { 
  TitleComponent, 
  TooltipComponent, 
  LegendComponent 
} from 'echarts/components';
import { CanvasRenderer } from 'echarts/renderers';

// 注册必要的组件
echarts.use([
  TitleComponent, 
  TooltipComponent, 
  LegendComponent,
  PieChart,
  CanvasRenderer
]);

const props = defineProps({
  title: {
    type: String,
    default: ''
  },
  data: {
    type: Array,
    required: true
  },
  radius: {
    type: Array,
    default: () => ['50%', '70%']
  },
  center: {
    type: Array,
    default: () => ['50%', '50%']
  },
  roseType: {
    type: Boolean,
    default: false
  },
  showLabel: {
    type: Boolean,
    default: true
  },
  legendPosition: {
    type: String,
    default: 'right'
  },
  colorList: {
    type: Array,
    default: () => []
  }
});

const chartRef = ref(null);
let chartInstance = null;

// 初始化图表
const initChart = () => {
  if (!chartRef.value) return;
  
  chartInstance = echarts.init(chartRef.value);
  updateChart();
  
  // 监听窗口变化，调整图表大小
  window.addEventListener('resize', handleResize);
};

// 更新图表数据和配置
const updateChart = () => {
  if (!chartInstance) return;
  
  const option = {
    title: {
      text: props.title,
      textStyle: {
        color: '#fff',
        fontSize: 14
      },
      left: 'center'
    },
    tooltip: {
      trigger: 'item',
      formatter: '{a} <br/>{b}: {c} ({d}%)'
    },
    legend: {
      orient: props.legendPosition === 'bottom' ? 'horizontal' : 'vertical',
      left: props.legendPosition === 'left' ? 'left' : (props.legendPosition === 'right' ? 'right' : 'center'),
      top: props.legendPosition === 'bottom' ? 'bottom' : 'middle',
      textStyle: {
        color: 'rgba(255, 255, 255, 0.7)'
      },
      icon: 'circle'
    },
    color: props.colorList.length > 0 ? props.colorList : [
      '#5470c6', '#91cc75', '#fac858', '#ee6666', '#73c0de', '#3ba272', '#fc8452', '#9a60b4', '#ea7ccc'
    ],
    series: [
      {
        name: props.title,
        type: 'pie',
        radius: props.radius,
        center: props.center,
        roseType: props.roseType ? 'radius' : false,
        avoidLabelOverlap: true,
        itemStyle: {
          borderRadius: 10,
          borderColor: 'rgba(0, 0, 0, 0.1)',
          borderWidth: 2
        },
        label: {
          show: props.showLabel,
          color: 'rgba(255, 255, 255, 0.7)'
        },
        labelLine: {
          show: props.showLabel,
          lineStyle: {
            color: 'rgba(255, 255, 255, 0.3)'
          }
        },
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: 'rgba(0, 0, 0, 0.5)'
          }
        },
        data: props.data
      }
    ]
  };

  chartInstance.setOption(option);
};

// 监听数据变化
watch(() => props.data, () => {
  updateChart();
}, { deep: true });

// 调整图表大小
const handleResize = () => {
  chartInstance && chartInstance.resize();
};

// 组件挂载时初始化图表
onMounted(() => {
  initChart();
});

// 组件卸载前销毁图表实例
onUnmounted(() => {
  if (chartInstance) {
    chartInstance.dispose();
    chartInstance = null;
  }
  window.removeEventListener('resize', handleResize);
});
</script>

<style scoped>
.chart-container {
  width: 100%;
  height: 100%;
  min-height: 300px;
}
</style> 